﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Data;

namespace Utilities
{
    public class FolderUtil
    {
        public static List<string> buildListFiles(string strFolderPath)
        {
            List<string> listFiles = new List<string>();
            string Dir = strFolderPath;
            ArrayList fils = new ArrayList();
            foreach (string file in Directory.GetFiles(Dir)) // add each file in directory
            {
                try{
                    string csvFileName = file;
                    if (csvFileName != null)
                    {
                        string strTestSuite = csvFileName.Replace(".csv", "");
                        listFiles.Add(strTestSuite);
                    }
                }catch{}
            }
            return listFiles;
        }

        public static DataTable buildDataTableOfFilesInFolder(string strFolderPath)
        {
            DataTable dtTarget = new DataTable();
            dtTarget.Columns.Add(new DataColumn("FileName", typeof(string)));
            List<string> listFiles = FolderUtil.buildListFiles(strFolderPath);
            for (int i = 0; i < listFiles.Count; i++)
            {
                DataRow dr = dtTarget.NewRow();
                string strFileItem = listFiles[i];
                string[] strs = strFileItem.Split('\\');
                strFileItem = strs[strs.Length - 1];

                if (!strFileItem.Equals("") && !strFileItem.Equals(" ") && !alreadyExisted(dtTarget, strFileItem))
                {
                    dr[0] = strFileItem;
                    dtTarget.Rows.Add(dr);
                }
            }
            return dtTarget;
        }

        private static Boolean alreadyExisted(DataTable dtListFavourite, string favouriteName)
        {
            foreach (DataRow dr in dtListFavourite.Rows)
            {
                string itemName = dr[0].ToString().Trim();
                if (itemName.Equals(favouriteName))
                {
                    return true;
                }
            }
            return false;
        }

        public static string createFolder(string strParentDirectory, string strExpectedFolder)
        {
            try
            {
                string activeDir = strParentDirectory;

                //Create a new subfolder under the current active folder
                string folderName = strExpectedFolder;
                string newPath = System.IO.Path.Combine(activeDir, folderName);

                // Create the subfolder
                System.IO.Directory.CreateDirectory(newPath);

                //Save to global variable
                return strParentDirectory + folderName + "\\";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        static public void CopyFolder(string sourceFolder, string destFolder )
            {
                if (!Directory.Exists( destFolder ))
                    Directory.CreateDirectory( destFolder );

                string[] files = Directory.GetFiles( sourceFolder );

                foreach (string file in files)
                {
                    string name = Path.GetFileName( file );
                    string dest = Path.Combine( destFolder, name );
                    File.Copy( file, dest );
                }
                
            string[] folders = Directory.GetDirectories( sourceFolder );

            foreach (string folder in folders)
            {
                string name = Path.GetFileName( folder );
                string dest = Path.Combine( destFolder, name );
                CopyFolder( folder, dest );
            }
        }
    }
}
